package com.myk.game.gobangchess.expand.bwantispace;

import java.util.ArrayList;
import java.util.List;

/**
 * 棋子-怪物-暴徒
 */
public class MonsterBT extends BaseMonsterPiece implements IGameInformation {
    public MonsterBT(Game game, int row, int col) {
        super(game, "暴徒", row, col);
        this.hp = 20;
        this.atk = 10;
    }

    @Override
    public void updateSelf() {
        //玩家在攻击范围内，3/4的几率进行攻击
        if (Math.random() < 0.75 && isAdjacentToPiece(game.human)) {
            this.attack(game.human);
            return;
        }
        //玩家在视野范围内，优先朝着玩家移动
        if (isInView(game.human) && !isAdjacentToPiece(game.human)) {
            int[] moveDir = getMoveDir(game.human);
            this.move(this.row + moveDir[0], this.col + moveDir[1]);
            return;
        }
        //否则随机游走
        moveRandom();
    }

    //目标棋子在自己的视野范围内
    public boolean isInView(BasePiece piece) {
        return game.helper.room.manager.getDistance(this.row, this.col, piece.row, piece.col) <= 6;
    }

    //根据目标棋子获取移动方向
    public int[] getMoveDir(BasePiece piece) {
        int[] dir = new int[]{0, 0};
        if (piece.row > this.row) {
            dir[0] = 1;
        }
        else if (piece.row < this.row) {
            dir[0] = -1;
        }
        if (piece.col > this.col) {
            dir[1] = 1;
        }
        else if (piece.col < this.col) {
            dir[1] = -1;
        }
        return dir;
    }

    //随机游走
    private void moveRandom() {
        //获取所有相邻位置的空格
        List<int[]> emptyPositions = new ArrayList<>();
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int newRow = row + i;
                int newCol = col + j;
                if (newRow >= 0 && newRow < game.helper.room.curGrids.length && newCol >= 0 && newCol < game.helper.room.curGrids[0].length && game.helper.room.curGrids[newRow][newCol] == 0) {
                    emptyPositions.add(new int[]{newRow, newCol});
                }
            }
        }
        if (emptyPositions.size() > 0) {
            int[] randomPosition = emptyPositions.get((int) (Math.random() * emptyPositions.size()));
            move(randomPosition[0], randomPosition[1]);
        }
    }

    @Override
    public String getInformation() {
        return "【怪物-暴徒】\n" +
                "生命值：" + this.hp + "\n" +
                "攻击力：" + this.atk + "\n";
    }
}